package leetcode.editor.cn;

import java.util.*;
import leetcode.editor.util.*;
public class ID2965FindMissingAndRepeatedValues{
	public static void main(String[] args) {
		Solution solution = new ID2965FindMissingAndRepeatedValues().new Solution();
		StringBuilder sb = new StringBuilder();
		
		//执行测试
		
		System.out.println(sb);		
	}
	//leetcode submit region begin(Prohibit modification and deletion)
	class Solution {
		private void add(int[] times, int n) {
			if (n > times[2]) {
				times[2] = n;
				if (times[2] > times[1]) {
					times[2] = times[2] ^ times[1] ^ (times[1] = times[2]);
					if (times[1] > times[0]) {
						times[1] = times[1] ^ times[0] ^ (times[0] = times[1]);
					}
				}
			}
		}

		private int getTimes(int[] times) {
			return Math.max(
					Math.min(times[0], Math.min(times[1], times[2])),
					Math.max(
							Math.min(times[0] - 1, times[1]),
							times[0] - 2
					)
			);
		}

		public int maximumLength(String s) {
			int[][] times = new int[26][3];
			int from = 0;
			for (int i = 1; i <= s.length(); i++) {
				if (i == s.length() || s.charAt(i) != s.charAt(i - 1)) {
					add(times[s.charAt(i - 1) - 'a'], i - from);
					from = i;
				}
			}
			int ans = 0;
			for (int i = 0; i < 26; i++) {
				ans = Math.max(ans, getTimes(times[i]));
			}
			return ans != 0 ? ans : -1;
		}
	}
//leetcode submit region end(Prohibit modification and deletion)
}
